package team.se.tms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import team.se.tms.constant.PathConstant;
import team.se.tms.dao.Dao;
import team.se.tms.dto.Page;
import team.se.tms.exception.UpdateException;
import team.se.tms.factory.ModelFactory;
import team.se.tms.po.CourseInfo;
import team.se.tms.po.TalentTrainingProgramInfo;
import team.se.tms.service.TalentTrainingProgramInfoService;
import team.se.tms.util.FileUtil;
import java.util.Date;
import java.util.List;

@Service("talentTrainingProgramInfoService")
public class TalentTrainingProgramInfoServiceImpl implements TalentTrainingProgramInfoService{
    @Autowired
    Dao dao;
    /**
     * 添加人才培养方案
     * @param multipartFile 文件
     * @param awardYear 年份
     * @return Boolean 添加结果
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @Override
    public Boolean saveTalentTrainingProgramInfo(MultipartFile multipartFile, Date awardYear) throws Exception {
        TalentTrainingProgramInfo talentTrainingProgramInfo =ModelFactory.createTalentProgramInfo(multipartFile);
        talentTrainingProgramInfo.setTalentTrainingProgramAwardYear(awardYear);
        if(dao.insert("TalentTrainingProgramInfoMapper.insertTalentTrainingProgramInfo",talentTrainingProgramInfo)>0){
            System.out.println("$$$$$$$$$************");
            return true;
        }
        System.out.println("$$$$$$$$$************");
        return false;
    }
    /**
     * 查询所有的人才培养方案
     *
     * @return List<TalentTrainingProgramInfo> 多个人才培养方案
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @Override
    public List<TalentTrainingProgramInfo> listTalentTrainingProgramInfos(Page page) throws Exception {
        List<TalentTrainingProgramInfo> talentTrainingProgramInfoList = null;
        talentTrainingProgramInfoList = dao.selectList("TalentTrainingProgramInfoMapper.selectTalentTrainingProgramInfos",page);

        return talentTrainingProgramInfoList;
    }
    /**
     * 查询人才培养方案是否被使用
     *
     * @param talentTrainingProgramId 人才培养方案Id
     * @return Boolean 使用结果
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @Override
    public Boolean isUsedTalentTrainingProgramInfo(String talentTrainingProgramId) throws Exception {
        List<CourseInfo> courseInfos =dao.selectList("CourseInfoMapper.selectCourseInfosByTalentTrainingProgramId",talentTrainingProgramId);
        //System.out.println("%^%^%^%^%^%^"+courseInfo);
        if(courseInfos==null||courseInfos.size()==0){
            System.out.println("111*(*(*(*(*(");
            return false;
        }else{
            System.out.println("*(*(*(*(*(");
            return true;
        }
    }
    /**
     * 修改人才培养方案
     * @param oldTalentTrainingProgramInfo  需要修改的人才培养方案信息
     * @param multipartFile 文件
     * @param awardYear 年份
     * @return Boolean 修改结果
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @Override
    public Boolean modifyTalentTrainingProgramInfo(TalentTrainingProgramInfo oldTalentTrainingProgramInfo,MultipartFile multipartFile, Date awardYear) throws Exception {

        TalentTrainingProgramInfo talentTrainingProgramInfo = null;
        if(this.isUsedTalentTrainingProgramInfo(oldTalentTrainingProgramInfo.getTalentTrainingProgramId())){
            throw new UpdateException("人才培养方案正在被使用");
        }
        if (multipartFile.isEmpty()){
            talentTrainingProgramInfo = oldTalentTrainingProgramInfo;
            talentTrainingProgramInfo.setTalentTrainingProgramAwardYear(awardYear);
        }else{
            talentTrainingProgramInfo =this.updateTalentProgramInfo(oldTalentTrainingProgramInfo.getTalentTrainingProgramId(),multipartFile,awardYear);
        }
        if(((int)dao.update("TalentTrainingProgramInfoMapper.updateTalentTrainingProgramInfoByTalentTrainingProgramId", talentTrainingProgramInfo))>0
                &&multipartFile!=null){
            //删除文件成功
            if(this.deleteTalentTrainingProgramFile(oldTalentTrainingProgramInfo.getTalentTrainingProgramAddress())){
                return true;
            }
        }
        return false;
    }
    /**
     * 删除人才培养方案
     *
     * @param talentTrainingProgramId 人才培养方案id
     * @param oldFilePath 旧的文件地址
     * @return Boolean 删除结果
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @Override
    public Boolean removeTalentTrainingProgramInfo(String talentTrainingProgramId,String oldFilePath) throws Exception {
        if(this.isUsedTalentTrainingProgramInfo(talentTrainingProgramId)){
            System.out.println("正在被使用");
            throw new UpdateException("人才培养方案正在被使用");
        }
        int result =dao.delete("TalentTrainingProgramInfoMapper.deleteTalentTrainingProgramInfoByTalentTraingProgramId",talentTrainingProgramId);
        System.out.println("&&&&&&&&&&&&&&&result"+result);
        if(result>0){
            if(this.deleteTalentTrainingProgramFile(oldFilePath)){
                return true;
            }
        }
        return false;
    }

    /**
     * 修改人才培养方案
     *
     * @param talentTrainingProgramId 人才培养方案id
     * @param multipartFile 文件
     * @return TalentTrainingProgramInfo 才培养方案信息类
     * @create on 2018/12/18 by johnny
     */
    public TalentTrainingProgramInfo updateTalentProgramInfo(String talentTrainingProgramId,MultipartFile multipartFile,Date awardYear){
        //获取文件名
        String name = FileUtil.GetFileNameWithoutExtension(multipartFile.getOriginalFilename());
        //文件二级路径
        String path =  PathConstant.TALENT_TRAINING_PROGRAM;
        //生成储存的文件名
        String fileName = talentTrainingProgramId+FileUtil.getExtension(multipartFile.getOriginalFilename());
        //生成文件地址
        String address = path+fileName;
        //存储文件
        FileUtil.storeFile(path, fileName, multipartFile);


        TalentTrainingProgramInfo talentTrainingProgramInfo = new TalentTrainingProgramInfo();
        talentTrainingProgramInfo.setTalentTrainingProgramName(name);
        talentTrainingProgramInfo.setTalentTrainingProgramId(talentTrainingProgramId);
        talentTrainingProgramInfo.setTalentTrainingProgramAddress(address);
        talentTrainingProgramInfo.setTalentTrainingProgramAwardYear(awardYear);
        return talentTrainingProgramInfo;
    }

    /**
     * 删除旧文件
     *
     * @param filePath 文件路径
     * @return Boolean 删除结果
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    public Boolean deleteTalentTrainingProgramFile(String filePath){
        boolean result = true;
        try{
            FileUtil.destroyFile(filePath);
        }catch (Exception e){
            result =false;
        }
        return result;
    }
}
